{include file="public/head"/}
<style>
    body {
        background-color: #ffffff;
    }
</style>

<body>
    <div class="layui-form layuimini-form">
        <input type="hidden" name="id" value="{$user.id}">
        <div class="layui-form-item">
            <label class="layui-form-label required">用户昵称</label>
            <div class="layui-input-block">
                <input type="text" name="nickname" lay-verify="required" lay-reqtext="请输入用户昵称" placeholder="请输入用户昵称"
                    value="{$user.nickname}" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">手机号码</label>
            <div class="layui-input-block">
                <input type="text" name="phone" placeholder="请输入手机号码"
                    value="{$user.phone}" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">头像</label>
            <div class="layui-input-inline">
                <input type="hidden" name="avatar" id="img2" value="{$user.avatar}">
                <button type="button" class="layui-btn layui-btn-sm layui-btn-normal" id="img">上传头像</button>
                <div class="layui-upload-list">
                    <img class="layui-upload-img" id="img1" src="{$user.avatar}" style="width:150px;">
                </div>
            </div>
            <div class="layui-form-mid layui-word-aux">
                尺寸250*250px
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-block">
                <input type="radio" name="sex" value="0" title="保密" {$user.sex==0?'checked':''}>
                <input type="radio" name="sex" value="1" title="男" {$user.sex==1?'checked':''}>
                <input type="radio" name="sex" value="2" title="女" {$user.sex==2?'checked':''}>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">用户余额</label>
            <div class="layui-input-inline">
                <input type="text" name="money" value="{$user.money}" class="layui-input" disabled>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">用户积分</label>
            <div class="layui-input-inline">
                <input type="text" name="score" value="{$user.score}" class="layui-input" disabled>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">开关</label>
            <div class="layui-input-block">
                <input type="checkbox" name="status" value="1" lay-skin="switch" lay-text="开启|关闭" {$user.status==1?'checked':''}>
            </div>
        </div>


        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
                <a href="javascript:;" class="layui-btn layui-btn-primary layui-border" lay-active="closePage">关闭</a>
            </div>
        </div>
    </div>
    </div>
    <script>
        layui.use(['form', 'upload', 'util'], function () {
            var form = layui.form,
                layer = layui.layer,
                upload = layui.upload,
                util = layui.util,
                $ = layui.$;

            //普通图片上传
            var uploadInst = upload.render({
                elem: '#img',
                url: "{:url('upFiles/upload')}",
                data: { width: 250, height: 250 },
                accept: 'images', //普通文件
                exts: 'jpg|png|gif|jpeg', //只允许上传压缩文件
                before: function () {
                    //开始加载效果
                    layui.use('layer', function () {
                        layui.layer.load();
                        $(".layui-layer-shade").css('background', '#000000')
                        $(".layui-layer-shade").css('opacity', '0.2')
                        $(".layui-layer-shade").click(function (event) {
                            event.stopPropagation();
                        })
                    })
                },
                done: function (res) {
                    //上传成功结束加载效果
                    layui.use('layer', function () {
                        layui.layer.closeAll();
                        setTimeout(function () {
                            layui.layer.closeAll();
                        }, 2000);
                    });
                    if (res.code === 1) {
                        $('#img1').attr('src', res.url);
                        $('#img2').val(res.url);
                        layer.msg(res.info, { icon: 1 });
                    } else {
                        layer.msg(res.msg, { icon: 5 });
                    }
                },
                error: function () {
                    //上传成功结束加载效果
                    layui.use('layer', function () {
                        layui.layer.closeAll();
                        setTimeout(function () {
                            layui.layer.closeAll();
                        }, 2000);
                    });
                    return layer.msg('上传失败', { icon: 2 });
                }
            });


            //监听提交
            form.on('submit(saveBtn)', function (data) {
                delete data.field.file;
                if(data.field.status == undefined) {
                    data.field.status = 0;
                }
                // 提交到方法 默认为本身
                $.post("", data.field, function (res) {
                    var loading = layer.load(1, {shade: [0.1, '#fff']});
                    layer.close(loading);
                    if (res.code > 0) {
                        layer.msg(res.msg, { time: 1800, icon: 1 });
                        // 关闭弹出层
                        setTimeout(() => {
                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.location.reload();//刷新父页面，注意一定要在关闭当前iframe层之前执行刷新
                            parent.layer.close(iframeIndex);
                        }, 1800);

                    } else {
                        layer.msg(res.msg, { time: 1800, icon: 2 });
                    }
                });
            });

            util.event('lay-active', {
                closePage: function () {
                    var iframeIndex = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(iframeIndex);
                }
            });
        });
    </script>
    {include file="public/footer"/}